package org.agridata.akvisualization.service.appoint.impl;


import org.agridata.akvisualization.doMain.appoint.dto.CcAaAndAbDto;
import org.agridata.akvisualization.mapper.appoint.CcAaAndAbMapper;
import org.agridata.akvisualization.result.Result;
import org.agridata.akvisualization.service.appoint.CcAaAndAbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CcAaAndAbServiceImpl implements CcAaAndAbService {

    @Autowired
    private CcAaAndAbMapper mapper;

    @Override
    public Result earTagsSelect(CcAaAndAbDto ccAaAndAbDto) {
        try {
            String[] earTags = ccAaAndAbDto.getEarTags().split(",");
            Set<String> earTagsSet = new HashSet<>(Arrays.asList(earTags)); // 将数组转换为List

            List<Map<String, Object>> mapList = mapper.selectEarTags();

            List<Map<String, Object>> whhList = mapper.selectWhhEarTags();
            List<Map<String, Object>> resultList = new ArrayList<>(); // 存储存在于 动物证mapList 中的元素
            List<Map<String, Object>> whhresultList = new ArrayList<>(); // 存储存在于 无害化 whhList 中的元素

            //动物证
            mapList.parallelStream()
                    .filter(map -> earTagsSet.stream().anyMatch(tag -> map.get("Eartags").toString().contains(tag)))
                    .forEach(resultList::add);
            //无害化
            whhList.parallelStream()
                    .filter(map -> earTagsSet.stream().anyMatch(tag -> map.get("Eartags").toString().contains(tag)))
                    .forEach(whhresultList::add);

            Set<Map<String, Object>> maps = new HashSet<>(resultList);
            Set<Map<String, Object>> whhmaps = new HashSet<>(whhresultList);
            // 对于 resultSet 中的每个 Map，只保留包含在 earTagsList 中的 "Eartags" 值
            maps.forEach(map -> {
                List<String> eartags = new ArrayList<>(Arrays.asList(map.get("Eartags").toString().split(","))); // 使用可修改的 ArrayList
                eartags.retainAll(earTagsSet); // 保留交集
                map.put("Eartags", String.join(",", eartags)); // 更新 "Eartags" 值
            });
            //无害化
            whhmaps.forEach(map -> {
                List<String> eartags = new ArrayList<>(Arrays.asList(map.get("Eartags").toString().split(","))); // 使用可修改的 ArrayList
                eartags.retainAll(earTagsSet); // 保留交集
                map.put("Eartags", String.join(",", eartags)); // 更新 "Eartags" 值
            });

            Map<String, Object> map = new HashMap<>();
            map.put("animal", new ArrayList<>(maps));
            map.put("whh", new ArrayList<>(whhmaps));
            return Result.ok(map);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("出现异常");
        }
    }

    @Override
    public Result belongVerify(CcAaAndAbDto ccAaAndAbDto) {
        try {
            String[] earTags = ccAaAndAbDto.getEarTags().split(",");
            Set<String> earTagsSet = new HashSet<>(Arrays.asList(earTags));
            List<Map<String, Object>> list = mapper.belongVerifyByFarMid(ccAaAndAbDto.getFarmMid());
            Set<String> earSet = new HashSet<>();
            for (Map<String, Object> map : list) {
                long stag = Long.parseLong(map.get("prefix").toString() + map.get("rangestart").toString());
                long etag = Long.parseLong(map.get("prefix").toString() + map.get("rangeend").toString());
                //根据号段算出单个耳标
                for (int i = 0; i <= etag - stag; i++) {
                    Long sss = stag + i;
                    earSet.add(sss.toString());
                }
            }
            earTagsSet.removeAll(earSet);
            return Result.ok(earTagsSet);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("出现异常");
        }


    }

    @Override
    public List<Map<String, String>> selectTCCAB() {
        List<Map<String, String>> list = mapper.selectTCCAA();
        return list;
    }

    @Override
    public void updateAb() {
        mapper.updateA();
        mapper.updateB();
    }
}
